var _treeNode,
	_node,//点击的节点
	optionType = "";
var setting = {
	view: {
		selectedMulti: false
	},
	edit: {
		enable: true,
		showRemoveBtn: false,
		showRenameBtn: false
	},
	data: {
		keep: {
			parent: true,
			leaf: true
		},
		simpleData: {
			enable: true
		}
	},
	callback: {
//		beforeDrag: beforeDrag,
//		beforeRemove: beforeRemove,
//		beforeRename: beforeRename,
//		onRemove: onRemove
		onClick: zTreeOnClick,//点击节点函数
	}
};

var zNodes = [
	{	
		pid:0,
		id:1,
		name: "父节点1 - 展开",
		icon_name:"aaa",
		open: true,
		url:"",
		children: [
			{
				pid:1,
				id:11,
				name: "叶子节点111",
				icon_name:"aaa",
				open: true,
				url:""
			},
			{
				pid:1,
				id:12,
				name: "叶子节点112",
				icon_name:"aaa",
				open: true,
				url:""
			},
		]
	}
];

var log, className = "dark";

/*
 * 点击节点回调函数
 * @param treeId {object} 树状菜单事件监听机制对象
 * @param treeNode {string} 树状菜单id值
 * @param node {obj} 所选择的树状菜单项的数据
 */
function zTreeOnClick(treeId, treeNode, node) {
	console.log(treeId, treeNode, node);
	_treeNode = treeNode;
	_node = node;
}

var newCount = 1;

//添加节点按钮
function add(e) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		isParent = e.data.isParent,
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		console.log(treeNode);
		optionType = "add";
	if(treeNode) {
		zTree.editName(treeNode[0]);
		$("#pid").val(treeNode.id).attr("readonly","readonly");
	} else {
		layer.msg("添加的是初级父节点");
	}
	$("#menuOption").modal("show");
	$("#form input:not(#pid)").val("");
};
//编辑
function edit() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		console.log(treeNode);
	if(nodes.length == 0) {
		layer.alert("请先选择一个节点");
		return;
	}
	optionType = "edit";
	$("#menuOption").modal("show");
	if(treeNode.menu_id == 0){//父节点
		$("#pid").val("").attr("readonly","readonly");
	}else{
		$("#pid").val(treeNode.menu_id).attr("readonly","readonly");
	}
	$("#name").val(treeNode.name);
	$("#icon_name").val(treeNode.icon_name);
	$("#url").val(treeNode.url);
	$("#show").val(treeNode.show);
	$('#form').append($('<input type="hidden" value="'+ treeNode.id +'" name="id"/>'));
};

//删除
function remove(e) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
	console.log(treeNode);	
	if(nodes.length == 0) {
		layer.alert("请先选择一个节点");
		return;
	}
	if(treeNode.children && treeNode.children.length > 0){
		layer.alert("有节点，不能直接删除父节点");
		return;
	}
	
	layer.confirm("确认删除？",function(){
		fnAjax.method_4_admin(
			url_join("admin/menu/delete"),
			{mid:treeNode.id},
			"post",
			function(data){
				console.log(data);
				layer.msg("操作成功",{time:1500},function(){
					window.location.reload();
				});
			}
		);
	});
	
//	var callbackFlag = $("#callbackTrigger").attr("checked");
//	zTree.removeNode(treeNode, callbackFlag);
};

//清空子节点
//function clearChildren(e) {
//	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
//		nodes = zTree.getSelectedNodes(),
//		treeNode = nodes[0];
//	if(nodes.length == 0 || !nodes[0].isParent) {
//		layer.alert("请先选择一个父节点");
//		return;
//	}
//	zTree.removeChildNodes(treeNode);
//};

//初始化树状菜单
function initList(){
	fnAjax.method_4_admin(
		url_join("admin/menu/list"),
		"",
		"post",
		function(data){
			console.log(data);
			$.each(data.data, function(i,v) {
				if(v.pid == 0 || !v.pid) v.isParent = true;
			});
			$.fn.zTree.init($("#treeDemo"), setting, data.data);
			
		}
	);
}

$(document).ready(function() {
	//初始化树状菜单插件
	initList();
	
	//监听所有增删查该事件
	$("#addParent").bind("click", {
		isParent: true
	}, add);
	$("#addLeaf").bind("click", {
		isParent: false
	}, add);
	$("#edit").bind("click", edit);
	$("#remove").bind("click", remove);
//	$("#clearChildren").bind("click", clearChildren);
	
	//初始化表单验证
		$('#form')
			.bootstrapValidator({
				message: '这个值是无效的',
				feedbackIcons: {
					valid: 'glyphicon glyphicon-ok',
					invalid: 'glyphicon glyphicon-remove',
					validating: 'glyphicon glyphicon-refresh'
				},
				fields: {							
					name: {
						message: '名称是无效的',
						validators: {
							notEmpty: {
								message: '名称是必需的，不能是空的'
							},
							stringLength: {
								min: 2,
								max: 20,
								message: '名称必须大于2，小于20个字符'
							},
						}
					},
					icon_name: {
						validators: {
							notEmpty: {
								message: '不能是空的'
							},	
							
						}
					},
					
				}
			})
			.on('success.form.bv', function(e) {
				// 阻止表单提交
				e.preventDefault();
				// 获得表单实例
				var $form = $(e.target);
				
				fnAjax.method_4_admin(
					(optionType == "add" ? url_join("admin/menu/create") : url_join("admin/menu/update")),
					$.serializeToObj($form.serialize()),
//					$form.serialize(),
					"post",
					function(data){
						console.log(data);
						layer.msg("操作成功",{time:1500},function(){
							window.location.reload();
						});
					}
				);
				
			});
		// 手动验证表单
		$('.btn-save').click(function() {
			$('#form').bootstrapValidator('validate');
		});
	
	
});

